package com.gaeainfo.modules.alarm.process.dao;

import com.gaeainfo.framework.core.utils.common.GaeaCommonUtil;
import com.gaeainfo.modules.alarm.process.entity.AlarmEntity;
import com.gaeainfo.modules.alarm.process.entity.AlarmProcessEntity;
import com.gaeainfo.modules.base.dao.BaseHibernateDao;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Caven on 2018/7/11.
 */

@Repository("alarmProcessDao")
public class AlarmProcessDaoImpl extends BaseHibernateDao implements AlarmProcessDao {

    @Override
    public Map<String, Object> findProcessDetailsById(String processId) {
        StringBuilder sqlBuilder = new StringBuilder();
        Map<String, Object> params = new HashMap<>();
        sqlBuilder.append(" select ap.alarm_id, ")
                .append("   to_char(ai.alarm_time, 'yyyy-mm-dd hh24:mi:ss') as alarm_time, ")
                .append("   ai.address, ")
                .append("   ai.alarm_person, ")
                .append("   ai.phone, ")
                .append("   ai.content, ")
                .append("   ai.dept_name, ")
                .append("   ai.type_desc, ")
                .append("   ap.pid as process_id, ")
                .append("   ap.dept_name as issued_dept_name, ")
                .append("   ap.status, ")
                .append("   dc.name as status_desc, ")
                .append("   ap.police_no, ")
                .append("   ap.police_name ")
                .append(" from t_alarm_process ap ")
                .append(" left join t_alarm_info ai ")
                .append("  on ap.alarm_id = ai.pid ")
                .append(" left join t_dic_code dc ")
                .append("  on ap.status = dc.code ")
                .append(" where dc.code_type = 'PROCESS_STATUS' ")
                .append(" and ap.pid = :processId ");
        params.put("processId", processId);
        List<Map<String, Object>> list = this.findBySql(sqlBuilder.toString(), params);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return new HashMap<>();
    }

    @Override
    public AlarmProcessEntity findAlarmProcessById(String processId) {
        return this.get(AlarmProcessEntity.class, processId);
    }

    @Override
    public AlarmEntity findAlarmById(String alarmId) {
        return this.get(AlarmEntity.class, alarmId);
    }

    @Override
    public String[] getParentDept(String deptId) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append(" select ad.pid as dept_id ")
                .append(" from t_alarm_dept ad ")
                .append(" start with ad.pid = :deptId ")
                .append(" connect by prior ad.parent_id = ad.pid ");
        Map<String, Object> params = new HashMap<>();
        params.put("deptId",deptId);
        List<Map<String, Object>> list = this.findBySql(sqlBuilder.toString(), params);
        if (list != null && !list.isEmpty()) {
            String[] deptIds = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                deptIds[i]=GaeaCommonUtil.trimToEmpty(list.get(i).get("DEPT_ID"));
            }
            return deptIds;
        }
        return new String[0];
    }

    @Override
    public void updateProcess(AlarmProcessEntity alarmProcessEntity) {
        this.update(alarmProcessEntity);
    }

    @Override
    public void updateAlarm(AlarmEntity alarmEntity) {
        this.update(alarmEntity);
    }
}
